package oa.hleast.xswl.controller.admin.blog;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.xswl.pojo.form.blog.BlogDisplayForm;
import oa.hleast.xswl.pojo.form.blog.ReviewForm;
import oa.hleast.xswl.pojo.vo.blog.UmsBlogCommentVo;
import oa.hleast.xswl.pojo.vo.blog.UmsBlogInfoVo;
import oa.hleast.xswl.service.blog.IBlogInfoService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "【系统管理】交流互动服务")
@RestController("AdminBlogController")
@RequestMapping("/api.admin/v1/blog")
@Slf4j
@AllArgsConstructor
public class BlogController {

    private final IBlogInfoService infoService;

    @ApiOperation(value = "博客审核 - 带token", httpMethod = "POST")
    @ApiImplicitParam(name = "form", value = "实体JSON对象", required = true, paramType = "body", dataType = "ReviewForm")
    @PostMapping("/reviewBlog")
    public Result<Boolean> reviewBlog(@RequestBody ReviewForm form) {
        boolean status = infoService.reviewBlog(form);
        return Result.judge(status);
    }

    @ApiOperation(value = "博客审核列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, paramType = "query", dataType = "Long")
    })
    @GetMapping("query-review-list")
    public Result<List<UmsBlogInfoVo>> list(
            @RequestParam @NonNull Integer page,
            @RequestParam @NonNull Integer limit) {
        Page<UmsBlogInfoVo> result = infoService.pageList(page, limit, null, null, null, false, true);
        return Result.success(result.getRecords(), result.getTotal());
    }

    @ApiOperation(value = "博客详情", httpMethod = "GET")
    @ApiImplicitParam(name = "blogId", value = "帖子的ID", required = true, paramType = "query", dataType = "Long")
    @GetMapping("/detailForUser")
    public Result<UmsBlogInfoVo> detailForUser(@RequestParam @NotNull Long blogId) {
        UmsBlogInfoVo vo = infoService.detail(blogId);
        return Result.success(vo);
    }

    @ApiOperation(value = "批量设置为热门贴", httpMethod = "POST")
    @ApiImplicitParam(name = "ids", value = "ids", required = true, paramType = "path", dataType = "String")
    @PostMapping("/setUpHotList/put/{ids}")
    public Result<Boolean> setUpHotList(@PathVariable("ids") String ids) {
        List<Long> idList = Arrays.stream(ids.split(",")).filter(ObjectUtils::isNotEmpty).map(Long::new).collect(Collectors.toList());
        boolean result = infoService.updateHotList(idList, true);
        return Result.judge(result);
    }

    @ApiOperation(value = "批量取消为热门贴", httpMethod = "POST")
    @ApiImplicitParam(name = "ids", value = "ids", required = true, paramType = "path", dataType = "String")
    @PostMapping("/cancelHotList/put/{ids}")
    public Result<Boolean> cancelHotList(@PathVariable("ids") String ids) {
        List<Long> idList = Arrays.stream(ids.split(",")).filter(ObjectUtils::isNotEmpty).map(Long::new).collect(Collectors.toList());
        boolean result = infoService.updateHotList(idList, false);
        return Result.judge(result);
    }

    @ApiOperation(value = "批量上架博客", httpMethod = "POST")
    @ApiImplicitParam(name = "form", value = "批量设置上架/下架表单", required = true, paramType = "body", dataType = "BlogDisplayForm")
    @PostMapping("/upDisplayList/put")
    public Result<Boolean> upDisplayList(@RequestBody BlogDisplayForm form) {

        boolean result = infoService.updateDisplayList(form, true);
        return Result.judge(result);
    }

    @ApiOperation(value = "批量下架博客", httpMethod = "POST")
    @ApiImplicitParam(name = "form", value = "批量设置上架/下架表单", required = true, paramType = "body", dataType = "BlogDisplayForm")
    @PostMapping("/downDisplayList/put")
    public Result<Boolean> downDisplayList(@RequestBody BlogDisplayForm form) {

        boolean result = infoService.updateDisplayList(form, false);
        return Result.judge(result);
    }

    @ApiOperation(value = "博客删除", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "贴子ID", required = true, paramType = "path", dataType = "Long"),
            @ApiImplicitParam(name = "delReason", value = "删除原因", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/delete/{id}/delBlog")
    public Result<Boolean> delBlog(@PathVariable @NotNull Long id, @RequestParam String delReason) {

        boolean result = infoService.delBlog(id, delReason);
        return Result.judge(result);
    }

    @ApiOperation(value = "评论列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式 list 分页查询", required = true, paramType = "query", dataType = "QueryModeEnum"),
            @ApiImplicitParam(name = "blogId", value = "帖子的ID", required = true, paramType = "query", dataType = "String")
    })
    @GetMapping("/commentList")
    public Result commentList(@RequestParam @NotBlank String queryMode,
                              @RequestParam @NotNull Long blogId) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            case LIST:
                List<UmsBlogCommentVo> result = infoService.findCommentList(blogId, null);
                return Result.success(result);
            default:
                throw new BizException("查询模式错误，请联系管理员");
        }
    }

}